package com.example.javamaday.leetcode;

import com.example.javamaday.leetcode.base.TreeNode;

public class LeetCodeTest0606 {

    public static void main(String[] args) {

    }

    private final StringBuilder result = new StringBuilder();

    public String tree2str(TreeNode root) {
        preorderTraversal(root);
        return result.toString();
    }

    public void preorderTraversal(TreeNode root) {
        if (root == null) {
            return;
        }
        result.append(root.val);
        if (root.left != null) {
            result.append("(");
            preorderTraversal(root.left);
            result.append(")");
        }
        if (root.right != null) {
            result.append("(");
            preorderTraversal(root.right);
            result.append(")");
        }
    }
}
